<?php

class Auth_model extends Onecore_Model {

    function __construct() {
        parent::__construct();
        $this->_name = '_resources';
        $this->_id = array('');
        $this->_itemName = '';
        $this->_validations = array(
            array(
                'field' => 'name',
                'label' => 'Nombre',
                'rules' => 'required'
            )
        );
       // $this->db = $this->checkDB($this->session->userdata('profile_in'),'ANR');
    }
    
    public function login($user,$pass){
        $sql = "select u.id, u.email, u.status, u._rols_id,  u.names, u.lastnames,
                group_concat( '{\"groups_id\":',rg.groups_id,
                                ',\"group_name\":\"',g.name,'\"',
                                ',\"group_type\":\"',tg.name,'\"',
                                ',\"_rols_id\":',rg._rols_id,                 
                                ',\"_rol_name\":\"',r.name,'\"}'
                ) as groups
                from users u, rols_groups rg , _rols r, groups g, typegroups tg
                where u.email = '$user'
                and u.password='$pass'
                and u.id = rg.users_id
                and rg._rols_id = r.id
                and rg.groups_id = g.id
                and tg.id = g.typegroups_id
                group by u.id
                order by g.name";         
        $query = $this->db->query($sql);  
        
//        echo $sql;exit;
        
        $result = $query->result();

   //     print_r($result);exit;

        if(!empty($result[0])){
//            
//            print_r($result[0])          ;
//            exit;
            return $result[0];
        }
        else return null;
    }
    
    public function hasPermission($controller,$action,$role){
        $sql = "select count(*) as count
                from    _resources re, _actions a, _rights ro
                where   re.name = '$controller'
                and     a._resources_id = re.id
                and     a.name = '$action'
                and     ro._actions_id = a.id
                and     ro._rols_id = $role limit 1";
        
//        echo $sql;exit;
        
        $query = $this->db->query($sql);  
        $result = $query->result();
        if(!empty($result[0])){
            return (bool)$result[0]->count;
        }
        return false;
    }
    
    public function groupHasModule($groupid,$moduleid){
        return true;
    }
    
    //obtiene los modulos configurados por grupo
    public function getModulesByGroup($groupId){
        $sql = "select * from modules where groups_id=$groupId order by name ASC";
        $query = $this->db->query($sql);
        return $query->result();
    }
    
    
    public function getPermitedViews($type,$role){
        $conditionType = '';
        if($type!=null){
            $conditionType = " and p.type = '".$type."' ";
        }

        $sql = "select  
                    re.id as controllerid,
                    re.description as controllername,
                    re.name as controller, 
                    a.name as action, 
                    a.description , 
                    a.type,
                    a.menuitem,
                    a.index
                from    
                    _actions a, _resources re, _rights r
                where   
                    r._rols_id = '$role' 
                    and r._actions_id = a.id
                    and a._resources_id = re.id
                order by re.id ASC, a.name ASC";

       // echo $sql;exit;

 //       log_message('error',$sql);

        $query = $this->db->query($sql);
        return $query->result();
    }
    public function getCategoryModules($rol){
        $arr = array();
        $sql = "SELECT *
                FROM tbl_modulos
                WHERE ModuloId
                IN (
                    SELECT m.parent_id
                    FROM  `tbl_modulos` m, tbl_pages p, tbl_systemrolspages r
                    WHERE r.SystemRolId =  '$rol'
                    AND r.PageId = p.PageId
                    AND p.ModuloId = m.ModuloId
                    AND m.parent_id
                    IN (
                        SELECT ModuloId
                        FROM tbl_modulos
                        WHERE parent_id =0
                    )
                    GROUP BY m.parent_id
                    ORDER BY m.order ASC
                );";
        
        //echo $sql;exit;
        
        
        $query = $this->db->query($sql);
        foreach ($query->result() as $obj) {
            $arr[] = $obj;
        }
        return $arr;
    }
    public function getChildModules($mid){
        $arr = array();
        $sql = "select p.ModuloId as controllerid,
                    p.Description as controllername,
                    p.name as controller
                from tbl_modulos p
                where p.parent_id = $mid
                order by p.order ASC;";
        $query = $this->db->query($sql);
        foreach ($query->result() as $obj) {
            $arr[] = $obj;
        }
        return $arr;
    }
    public function getPagesByModule($mid){
        $arr = array();
        $sql = "select p.name as action, p.Description, p.type
                from tbl_pages p
                where p.ModuloId = $mid
                and p.type = 'view'";
        $query = $this->db->query($sql);
        foreach ($query->result() as $obj) {
            $arr[] = $obj;
        }
        return $arr;
    }
    public function getPermitedViews_sql($type,$role){
        $arr = array();
        $conditionType = '';
        $categorias = $this->getCategoryModules($role);
        
        //print_r($categorias);
        
        foreach ($categorias as $category) {
            $category->moduleChilds = $this->getChildModules($category->ModuloId);
            foreach ($category->moduleChilds as $module) {
                $module->items = $this->getPagesByModule($module->controllerid);
            }
            $arr[] =  $category;
        }
        return $arr;
    }
}